// pages/campManage/campManage.js
const http = require("../../api/camp")
const {baseURL} = require("../../utils/request")
import Dialog from '@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    campList: [],
    oss_url: baseURL,
    statuses: [
      { id: 2, status: '待审核', isActive: false }, 
      { id: 3, status: '审核通过', isActive: false },  
      { id: 4, status: '审核失败', isActive: false },  
    ],
    userid:1,
    usertype:null,
    state: null,
    approve: null
  },
  // 点击tab栏切换
  onChange(event) {
    console.log("active",event)
    this.setData({
      usertype: event.detail.name
    })
    if(this.data.usertype==0){
      let data = {
        userid: this.data.userid
      }
      this.getCampList(data)
    }else{
      let data = {
        userid: this.data.userid,
        usertype:this.data.usertype
      }
      // 每次点击tab调用接口
      this.getCampList(data)
    }
  },
  // 获取营地列表
  getCampList(data){
    http.campList(data).then(res => {
      console.log(res.data,"营地列表")
      if(res.code == 200){
        res.data.forEach(item=>{
          // 判断有没有逗号 有就是多张图片 取第一张图片
          if(item.imageUrl.indexOf(',') != -1){
            item.images = item.imageUrl.split(',');
            item.imageUrl = item.images[0];
          }
        });
        this.setData({
          campList: res.data
        });
      }
        
    });
  },
  // 点击筛选的按钮
  handleStatusTap(e) {  
    const id = e.currentTarget.dataset.id;  
    
    // 处理点击选中状态  
    const newStatuses = this.data.statuses.map(status => ({  
      ...status,  
      isActive: status.id === id, 
    }));  
    let state = null;  
    let approve = null;  
    if (id === 1) {  
      state = 1; 
    } else if (id >= 2 && id <= 4) {  
      approve = id - 2; // 2对应0, 3对应1, 4对应2  
    }  
    this.setData({  
      statuses: newStatuses,  
      state: state,  
      approve: approve
    });  
    if(this.data.usertype==0){
      let data ={
        userid:this.data.userid,
        approve:this.data.approve
      }
      this.getCampList(data)
    }else{
      let data ={
        userid:this.data.userid,
        usertype:this.data.usertype,
        approve:this.data.approve
      }
      this.getCampList(data)
    }
  },
  // 删除营地
  delCamp(e){
    const {campId} = e.currentTarget.dataset;
    Dialog.confirm({
      title: '营地删除',
      message: '您确定删除该营地吗？',
    }).then(() => {
        http.deleteCamp(campId).then(res=>{
          if(res.code == 200){
            this.getCampList()
          }
        })
      }).catch(() => {
        // on cancel
        console.log("取消删除了");
      });
  },
  // 查看营地详情
  campDetail(e){
    const {campId} = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/detail/detail?id=${campId}`,
    })
  },
  // 其他设置
  otherSet(e){
    const {campId} = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/otherManage/otherManage?campId=${campId}`,
    })
  },
  // 修改
  editorCamp(e){
    const {campId} = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/modifyCamp/modifyCamp?campId=${campId}`,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getCampList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})